#====# Appendix K: Robustness to alternative matching methods #====#

# Load libraries and set defaults ----
library(bizdays)
library(modelsummary)
library(tinytable)
library(tidyverse)
library(tidylog, warn.conflicts = FALSE)
source("aux/event_function.R")

# prepare business calendar:
business_calendar <- create.calendar('biz_calendar', weekdays = c('saturday','sunday'))

# Import data ----
stocks <- read_rds("data_out/stocks_analysis.rds") # main analysis dataset
stocks_cem <- read_rds("data_out/stocks_analysis_CEM.rds") # CEM-dataset
stocks_ent <- read_rds("data_out/stocks_analysis_entropy.rds") # entropy weighting-dataset
match <- read_rds("data_out/matching.rds") # list with matching results

# Table K.1: Building a placebo set of non-FCPA targets with coarsened exact matching ----
est <- return_daily_avg(data = stocks_cem %>%
                          filter(ticker_symbol %in% match$coar_ex_ma$ticker_symbol), 
                        abn_ret = "abn_chg", 
                        cum_abn_ret = "car", stratum = "FCPA_sample") %>%
  mutate(date = format(date, format = "%a, %b %d %Y")) %>%
  rename("term" = "date")

mod_ar1 <- list(tidy = est %>%
                  filter(FCPA_sample == "1" &
                           dv == "ar") %>%
                  select(-FCPA_sample, -dv),
                glance = est %>%
                  filter(FCPA_sample == "1" &
                           dv == "ar") %>%
                  filter(term == "Mon, Feb 10 2025") %>%
                  select(N) %>%
                  rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_car1 <- list(tidy = est %>%
                   filter(FCPA_sample == "1" &
                            dv == "car") %>%
                   select(-FCPA_sample, -dv),
                 glance = est %>%
                   filter(FCPA_sample == "1" &
                            dv == "car") %>%
                   filter(term == "Mon, Feb 10 2025") %>%
                   select(N) %>%
                   rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_ar0 <- list(tidy = est %>%
                  filter(FCPA_sample == "0" &
                           dv == "ar") %>%
                  select(-FCPA_sample, -dv),
                glance = est %>%
                  filter(FCPA_sample == "0" &
                           dv == "ar") %>%
                  filter(term == "Mon, Feb 10 2025") %>%
                  select(N) %>%
                  rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_car0 <- list(tidy = est %>%
                   filter(FCPA_sample == "0" &
                            dv == "car") %>%
                   select(-FCPA_sample, -dv),
                 glance = est %>%
                   filter(FCPA_sample == "0" &
                            dv == "car") %>%
                   filter(term == "Mon, Feb 10 2025") %>%
                   select(N) %>%
                   rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_ar_diff <- list(tidy = est %>%
                      filter(FCPA_sample == "diff" &
                               dv == "ar") %>%
                      select(-FCPA_sample, -dv),
                    glance = est %>%
                      filter(FCPA_sample == "diff" &
                               dv == "ar") %>%
                      filter(term == "Mon, Feb 10 2025") %>%
                      select(N) %>%
                      rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_car_diff <- list(tidy = est %>%
                       filter(FCPA_sample == "diff" &
                                dv == "car") %>%
                       select(-FCPA_sample, -dv),
                     glance = est %>%
                       filter(FCPA_sample == "diff" &
                                dv == "car") %>%
                       filter(term == "Mon, Feb 10 2025") %>%
                       select(N) %>%
                       rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

modelsummary(list("(1) \\textsc{ar}" = mod_ar1,
                  "(2) \\textsc{car}" = mod_car1,
                  "(3) \\textsc{ar}" = mod_ar0,
                  "(4) \\textsc{car}" = mod_car0,
                  "(5) \\textsc{ar}" = mod_ar_diff,
                  "(6) \\textsc{car}" = mod_car_diff),
             # statistic = "[{conf.low}, {conf.high}]",
             notes = paste0("Average \\textsc{ar} and \\textsc{car} to past FCPA targets and matched placebo firms per day. ",
                            "Matched placebo sample of non-FCPA targets built with coarsened exact matching. ",
                            "Standard errors of the mean reported in parentheses. P-values from a two-tailed test ",
                            "of difference from zero for the average against a standard normal distribution. ",
                            "Estimation window starts 30 days and ends 5 days before FCPA Executive Order. Market models estimated using ",
                            "the LASSO and individual S\\&P 500 constituents as predictors, selected using 15-fold cross validation. ",
                            "Columns 5 and 6 report the difference in means, respectively, between averages in columns 1 and 3, and those in columns 2 and 4.",
                            collapse = ""),
             title = "Building a placebo set of non-FCPA targets with coarsened exact matching \\label{tab:rob_coarsened}",
             stars = c("*" = 0.05), 
             escape = FALSE) %>%
  group_tt(j = list("Past FCPA targets" = 2:3,
                    "Non-FCPA targets" = 4:5,
                    "Difference-in-means" = 6:7)) %>%
  group_tt(i = list("Pre-event:" = 1,
                    "Post-event:" = 11)) %>%
  style_tt(i = 21, line_color = "white", line_width = 0.1, line = "t") %>%
  style_tt(i = 22, line_color = "black", line_width = 0.05, line = "b") %>%
  theme_tt("resize", width = .9) %>%
  theme_tt("placement", latex_float = "!htbp") %>%
  save_tt("tables/table_K1.html", overwrite = TRUE)

# Table K.2: Weighing placebo set of non-FCPA targets with entropy balancing ----
est <- return_daily_avg(data = stocks_ent %>%
                          filter(!is.na(entropy_weight)) %>%
                          mutate(w_abn_chg = abn_chg * entropy_weight,
                                 w_car = car * entropy_weight), 
                        abn_ret = "w_abn_chg", cum_abn_ret = "w_car", stratum = "FCPA_sample") %>%
  mutate(date = format(date, format = "%a, %b %d %Y")) %>%
  rename("term" = "date")

mod_ar1 <- list(tidy = est %>%
                  filter(FCPA_sample == "1" &
                           dv == "ar") %>%
                  select(-FCPA_sample, -dv),
                glance = est %>%
                  filter(FCPA_sample == "1" &
                           dv == "ar") %>%
                  filter(term == "Mon, Feb 10 2025") %>%
                  select(N) %>%
                  rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_car1 <- list(tidy = est %>%
                   filter(FCPA_sample == "1" &
                            dv == "car") %>%
                   select(-FCPA_sample, -dv),
                 glance = est %>%
                   filter(FCPA_sample == "1" &
                            dv == "car") %>%
                   filter(term == "Mon, Feb 10 2025") %>%
                   select(N) %>%
                   rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_ar0 <- list(tidy = est %>%
                  filter(FCPA_sample == "0" &
                           dv == "ar") %>%
                  select(-FCPA_sample, -dv),
                glance = est %>%
                  filter(FCPA_sample == "0" &
                           dv == "ar") %>%
                  filter(term == "Mon, Feb 10 2025") %>%
                  select(N) %>%
                  rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_car0 <- list(tidy = est %>%
                   filter(FCPA_sample == "0" &
                            dv == "car") %>%
                   select(-FCPA_sample, -dv),
                 glance = est %>%
                   filter(FCPA_sample == "0" &
                            dv == "car") %>%
                   filter(term == "Mon, Feb 10 2025") %>%
                   select(N) %>%
                   rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_ar_diff <- list(tidy = est %>%
                      filter(FCPA_sample == "diff" &
                               dv == "ar") %>%
                      select(-FCPA_sample, -dv),
                    glance = est %>%
                      filter(FCPA_sample == "diff" &
                               dv == "ar") %>%
                      filter(term == "Mon, Feb 10 2025") %>%
                      select(N) %>%
                      rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

mod_car_diff <- list(tidy = est %>%
                       filter(FCPA_sample == "diff" &
                                dv == "car") %>%
                       select(-FCPA_sample, -dv),
                     glance = est %>%
                       filter(FCPA_sample == "diff" &
                                dv == "car") %>%
                       filter(term == "Mon, Feb 10 2025") %>%
                       select(N) %>%
                       rename("N of firms" = "N")) %>%
  `class<-`("modelsummary_list")

modelsummary(list("(1) \\textsc{ar}" = mod_ar1,
                  "(2) \\textsc{car}" = mod_car1,
                  "(3) \\textsc{ar}" = mod_ar0,
                  "(4) \\textsc{car}" = mod_car0,
                  "(5) \\textsc{ar}" = mod_ar_diff,
                  "(6) \\textsc{car}" = mod_car_diff),
             # statistic = "[{conf.low}, {conf.high}]",
             notes = paste0("Average \\textsc{ar} and \\textsc{car} to past FCPA targets and matched placebo firms per day. ",
                            "Non-FCPA targets include full set of non-FCPA targets US-traded firms with covariate information, outcomes reweighted with entropy balancing. ",
                            "Standard errors of the mean reported in parentheses. P-values from a two-tailed test ",
                            "of difference from zero for the average against a standard normal distribution. ",
                            "Estimation window starts 30 days and ends 5 days before FCPA Executive Order. Market models estimated using ",
                            "the LASSO and individual S\\&P 500 constituents as predictors, selected using 15-fold cross validation. ",
                            "Columns 5 and 6 report the difference in means, respectively, between averages in columns 1 and 3, and those in columns 2 and 4.",
                            collapse = ""),
             title = "Weighing placebo set of non-FCPA targets with entropy balancing \\label{tab:rob_entropy}",
             stars = c("*" = 0.05), 
             escape = FALSE) %>%
  group_tt(j = list("Past FCPA targets" = 2:3,
                    "Non-FCPA targets" = 4:5,
                    "Difference-in-means" = 6:7)) %>%
  group_tt(i = list("Pre-event:" = 1,
                    "Post-event:" = 11)) %>%
  style_tt(i = 21, line_color = "white", line_width = 0.1, line = "t") %>%
  style_tt(i = 22, line_color = "black", line_width = 0.05, line = "b") %>%
  theme_tt("resize", width = .9) %>%
  theme_tt("placement", latex_float = "!htbp") %>%
  save_tt("tables/table_K2.html", overwrite = TRUE)

#====# The End #====#